<?php

namespace app\api\common;

use think\exception\Handle;
use think\facade\Log;
use think\Response;
use Throwable;

class ExceptionHandle extends Handle
{
    public function render($request, Throwable $e): Response
    {
        // 如果非后台,则返回json
        if(app()->http->getName() != 'admin') {
            Log::error(sprintf("%s in %s: %s", $e->getMessage(), $e->getFile(), $e->getLine()));
            $data = \app\api\extend\Response::setError('网络错误,请重试');
            return json($data)->code(503);
        }

        // 如果使用ajax请求,则返回json
        if($request->isJson() or $request->isAjax(true)) {
            Log::error(sprintf("%s in %s: %s", $e->getMessage(), $e->getFile(), $e->getLine()));
            $data = \app\api\extend\Response::setError('网络错误,请重试');
            return json($data)->code(503);
        }

        // 走父类逻辑
        return parent::render($request, $e); // TODO: Change the autogenerated stub
    }
}